﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vectorise
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
        }

        public void Run()
        {
            int trials = 13000;
            int runs = 10;
            int obs = 1424;
            int unds = 5;

            Console.WriteLine("C# trials={0} nUnd={1} nObs={2} order={3}", trials, unds, obs, trials*unds*obs);
            S0 = new double[trials];
            Random r = new Random();
            int t = Environment.TickCount;
            for (int i = 0; i < trials; ++i)
                S0[i] = 100 + 20 * (r.NextDouble() - 0.5);
            Console.WriteLine("RNG Time: {0}", Environment.TickCount - t);

            userPayoff.userFunctions payoff = new userPayoff.userFunctions();
            payoff.init(trials, unds, obs);
            payoff.initData();

            //JIT first
            payoff.calcCorePayoffBi(0);
            
            Strike = 100;
            Fair = new double[trials];

            //for (int bucketsize = 10; bucketsize <= maxbucketsize; bucketsize *= 10)
            int sum = 0, sumSq = 0;
            for (int run = 0; run < runs; ++run)
            {
                //t = Environment.TickCount;
                //for (int buckets = 0; buckets < trials / bucketsize; ++buckets)
                //    for (int i = 0; i < bucketsize; ++i)
                //        EuropeanSingle(i);
                //Console.Write("F1 BucketSize: {0}\tEuroSingle {1}\t", bucketsize, Environment.TickCount - t);

                t = Environment.TickCount;
                //for (int buckets = 0; buckets < trials / bucketsize; ++buckets)
                for (int i = 0; i < trials; ++i)
                    {
                        payoff.calcCorePayoffBi(i);
                    }
                int ms = Environment.TickCount - t;
                sum += ms;
                sumSq += ms*ms;
            }
            double mean = sum / runs;
            double sd = Math.Sqrt(sumSq / runs - mean*mean);

            Console.WriteLine("Payoff \t{0} +/- {1}", mean, sd);

                //for (int buckets = 0; buckets < trials / bucketsize; ++buckets)
                //    EuropeanVector(bucketsize);
                //Console.WriteLine("EuroVector{0}", Environment.TickCount - t);
        }

        public double[] S0 { get; set; }
        public double Strike { get; set;  }
        public double[] Fair { get; set; }

        public void EuropeanSingle(int i)
        {
            Fair[i] = Math.Max(S0[i] - Strike, 0.0);
        }

        public void EuropeanVector(int bucketsize)
        {
            for(int i=0; i<bucketsize; ++i)
                Fair[i] = Math.Max(S0[i] - Strike, 0.0);
        }
    }
}
